package
{
	import com.kitchen.AbstractModule;
	import com.kitchen.KitchenEvent;
	import com.kitchen.npc.NpcContainer;
	import com.kitchen.ui.Icon.CookIcon;
	import com.kitchen.ui.button.NoticeButton;
	import com.kitchen.ui.button.SettingButton;
	import com.kitchen.ui.controls.AccountBar;
	import com.kitchen.ui.controls.FriendsBar;
	import com.kitchen.ui.controls.LeftToolBar;
	import com.kitchen.ui.controls.Portrait;
	import com.kitchen.ui.controls.SettingBar;
	import com.kitchen.ui.controls.start.StartBar;
	import com.kitchen.ui.controls.StockBar;
	import com.kitchen.ui.deco.BackGround;
	import com.kitchen.ui.deco.CounterContainer;
	import com.kitchen.ui.deco.FloorContainer;
	import com.kitchen.ui.deco.WallContainer;
	import com.kitchen.ui.dialog.AbstractDialog;
	import com.kitchen.ui.dialog.decorate.DecorateDialog;
	import com.kitchen.ui.dialog.rank.RankDialog;
	import com.kitchen.ui.dialog.shop.ShopDialog;
	import com.kitchen.ui.dialog.storage.StorageDialog;
	import com.kitchen.ui.dialog.task.TaskDialog;
	import com.kitchen.ui.dialog.words.LeaveWordsDialog;
	
	import flash.display.Bitmap;
	import flash.events.Event;
	
	public class MasterSprite extends AbstractModule
	{
		//for test
		public var w:Number;
		public var h:Number;
		
		private var npc:NpcContainer;
		private var wall:WallContainer;
		private var floor:FloorContainer;
		private var counter:CounterContainer;
		private var topBox:AccountBar;
		private var friendBar:FriendsBar;
		private var leftBox:LeftToolBar;
		private var startBox:StartBar;
		private var stockBox:StockBar;
		private var dialog:AbstractDialog = null;
		
		public function MasterSprite(){
			super();
			this.w = 779;
			this.h = 644;
		}
		
		override protected function layout():void{
			//for test
			this.wall = new WallContainer(this.w, 272);
			this.wall.append({
				'id':30,
				'nCrTime':1321005709,
				'nDid':1,
				'nExpTime':0,
				'nPosit':1,
				'nPx':60,
				'nPy':80,
				'nPz':0,
				'nShopId':131,
				'nStatus':2,
				'nUid':1,
				'nUseTime':0,
				'sName':'沙发',
				'sUrl':'/static/kitchen/deco/zx08_q_msj.png'
			});
			this.addChild(this.wall);
			this.floor = new FloorContainer(this.w, 100);
			this.floor.y = this.wall.h;
			this.floor.append({
				'id':30,
				'nCrTime':1321005709,
				'nDid':1,
				'nExpTime':0,
				'nPosit':3,
				'nPx':60,
				'nPy':80,
				'nPz':0,
				'nShopId':131,
				'nStatus':2,
				'nUid':1,
				'nUseTime':0,
				'sName':'沙发',
				'sUrl':'/static/kitchen/deco/zx05_d_msj.png'
			});
			this.addChild(this.floor);
			this.npc = new NpcContainer(this.shop);
			this.npc.y = 260;
			this.npc.x = 0;
			this.npc.addEventListener(NpcContainer.BOUT_END, clickHandle);
			this.addChild(this.npc);
			this.counter = new CounterContainer(this.w, 289);
			this.counter.y = this.h-289;
			this.counter.append({
				'id':30,
				'nCrTime':1321005709,
				'nDid':1,
				'nExpTime':0,
				'nPosit':6,
				'nPx':60,
				'nPy':80,
				'nPz':0,
				'nShopId':131,
				'nStatus':2,
				'nUid':1,
				'nUseTime':0,
				'sName':'沙发',
				'sUrl':'/static/kitchen/deco/zx14_t_msj.png'
			});
			this.addChild(this.counter);
			
			var noticeButton:NoticeButton = new NoticeButton(this.mainSkin);
			noticeButton.addEventListener(NoticeButton.S_WORDS, clickHandle);
			noticeButton.x = 10;
			noticeButton.y = 65;
			this.addChild(noticeButton);
			var cookIcon:CookIcon = new CookIcon(this.mainSkin);
			cookIcon.x = 10;
			cookIcon.y = 105;
			this.addChild(cookIcon);
			this.topBox = new AccountBar(this.mainSkin, this.shoper, this.shops, this.shop);
			this.addChild(this.topBox);
			this.friendBar = new FriendsBar(this.mainSkin, this.shoper,this.shop);
			this.leftBox = new LeftToolBar(this.mainSkin);
			this.leftBox.addEventListener(LeftToolBar.S_CROWN, clickHandle);
			this.leftBox.addEventListener(LeftToolBar.S_DECORATE, clickHandle);
			this.leftBox.addEventListener(LeftToolBar.S_DIARY, clickHandle);
			this.leftBox.addEventListener(LeftToolBar.S_FOOD, clickHandle);
			this.leftBox.addEventListener(LeftToolBar.S_SHOP, clickHandle);
			this.leftBox.addEventListener(LeftToolBar.S_TOOL, clickHandle);
			this.topBox.x = 0;
			this.topBox.y = 5;
			this.friendBar.y = 130;
			this.leftBox.x = 0;
			this.leftBox.y = 150;
			this.addChild(this.friendBar);
			this.addChild(this.leftBox);
			
			this.startBox = new StartBar(this.mainSkin);
			this.startBox.x = this.w-this.startBox.w-5;
			this.startBox.y = this.h-this.startBox.h-5;
			this.startBox.showGuest(58);
			this.startBox.addEventListener(KitchenEvent.START, clickHandle);
			this.addChild(this.startBox);
			
			this.stockBox = new StockBar(this.mainSkin, this.shoper, this.shop);
			this.stockBox.x = 10;
			this.stockBox.y = this.h-this.stockBox.h-10;
			this.addChild(this.stockBox);
			
			var settingBar:SettingBar = new SettingBar(this.mainSkin, this.shop);
			settingBar.x = 738;
			settingBar.y = 3;
			this.addChild(settingBar);
		}
		
		private function clickHandle(event:Event):void{
			if(this.dialog != null){
				return;
			}
			switch(event.type){
				case KitchenEvent.START:
					if(this.npc.getState() == 0){
						this.npc.start();
						this.startBox.start();
					}
					break;
				case NpcContainer.BOUT_END:
					this.startBox.stop();
					break;
				case Portrait.E_INFO:
				this.dialog = (new RankDialog(this.mainSkin, this.dialogSkin, this.shoper, this.shop)) as AbstractDialog;
				this.addChild(this.dialog);
				this.dialog.show({});
				break;
				case NoticeButton.S_WORDS:
				this.dialog = (new LeaveWordsDialog(this.mainSkin,this.dialogSkin, this.shoper, this.shop)) as AbstractDialog;
				this.addChild(this.dialog);
				this.dialog.show({});
				break;
				//装修
				case LeftToolBar.S_DECORATE:
				this.dialog = (new DecorateDialog(this.mainSkin, this.dialogSkin, this.shoper, this.shop)) as AbstractDialog;
				this.addChild(this.dialog);
				this.dialog.show({});
				break;
				//任务
				case LeftToolBar.S_DIARY:
				this.dialog = (new TaskDialog(this.mainSkin, this.dialogSkin, this.shoper, this.shop)) as AbstractDialog;
				this.addChild(this.dialog);
				this.dialog.show({});
				break;
				//储物盒
				case LeftToolBar.S_TOOL:
				this.dialog = (new StorageDialog(this.mainSkin,this.dialogSkin, this.shoper, this.shop)) as AbstractDialog;
				this.addChild(this.dialog);
				this.dialog.show({});
				break;
				//游戏图标 食物信息和客人信息
				case LeftToolBar.S_FOOD:
				
				break;
				//商店
				case LeftToolBar.S_SHOP:
				this.dialog = (new ShopDialog(this.mainSkin, this.dialogSkin, this.shoper, this.shop)) as AbstractDialog;
				this.addChild(this.dialog);
				this.dialog.show({});
				break;
				//旁行帮
				case LeftToolBar.S_CROWN:
				this.dialog = (new RankDialog(this.mainSkin, this.dialogSkin, this.shoper, this.shop)) as AbstractDialog;
				this.addChild(this.dialog);
				this.dialog.show({});
				break;
				case KitchenEvent.START:
				if(!this.startBox.state){
					this.startBox.start();
					this.startBox.showGuest(62);
					this.startBox.showTime(12,34);
				}
				break;
				default:
				break;
			}
			if(this.dialog != null){
				this.dialog.addEventListener(AbstractDialog.DIALOG_CLOSE, closeDialogHandle);
				this.dialog.addEventListener(AbstractDialog.DIALOG_SURE, sureDialogHandle);
			}
			
		}
		
		private function closeDialogHandle(event:Event):void{
			this.dialog.removeEventListener(AbstractDialog.DIALOG_CLOSE, closeDialogHandle);
			this.dialog.removeEventListener(AbstractDialog.DIALOG_SURE, sureDialogHandle);
			this.removeChild(this.dialog);
			this.dialog = null;
		}
		
		private function sureDialogHandle(event:Event):void{
			this.dialog.close();
		}
	}
}